{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Cheat Sheet\n",
    "\n",
    "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n",
    "\n",
    "## Read It\n",
    "\n",
    "- [Website](https://www.pythoncheatsheet.org)\n",
    "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n",
    "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n",
    "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n",
    "\n",
    "## JSON, YAML and configuration files\n",
    "\n",
    "### JSON\n",
    "\n",
    "Open a JSON file with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "with open(\"filename.json\", \"r\") as f:\n",
    "    content = json.loads(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a JSON file with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "content = {\"name\": \"Joe\", \"age\": 20}\n",
    "with open(\"filename.json\", \"w\") as f:\n",
    "    f.write(json.dumps(content, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### YAML\n",
    "\n",
    "Compared to JSON, YAML allows a much better humain maintainance and gives ability to add comments.\n",
    "It is a convinient choice for configuration files where human will have to edit.\n",
    "\n",
    "There are two main librairies allowing to access to YAML files:\n",
    "\n",
    "- [PyYaml](https://pypi.python.org/pypi/PyYAML)\n",
    "- [Ruamel.yaml](https://pypi.python.org/pypi/ruamel.yaml)\n",
    "\n",
    "Install them using `pip install` in your virtual environment.\n",
    "\n",
    "The first one it easier to use but the second one, Ruamel, implements much better the YAML\n",
    "specification, and allow for example to modify a YAML content without altering comments.\n",
    "\n",
    "Open a YAML file with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ruamel.yaml import YAML\n",
    "\n",
    "with open(\"filename.yaml\") as f:\n",
    "    yaml=YAML()\n",
    "    yaml.load(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Anyconfig\n",
    "\n",
    "[Anyconfig](https://pypi.python.org/pypi/anyconfig) is a very handy package allowing to abstract completly the underlying configuration file format. It allows to load a Python dictionary from JSON, YAML, TOML, and so on.\n",
    "\n",
    "Install it with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "pip install anyconfig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Usage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import anyconfig\n",
    "\n",
    "conf1 = anyconfig.load(\"/path/to/foo/conf.d/a.yml\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
